package com.zg.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zg.pojo.Categorize;
import com.zg.service.CategorizeService;

@Controller
@RequestMapping("/categorize")
public class CategorizeController {
	@Resource
	private CategorizeService categorizeService;
	
	@RequestMapping(value="/editcategorize", method=RequestMethod.GET)
	public String editCategorizeForm(HttpServletRequest request,Model model){
		int categorizeId = Integer.parseInt(request.getParameter("id"));
		Categorize categorize = this.categorizeService.getCategorizeById(categorizeId);
		model.addAttribute("categorize", categorize);
		return "categorize/editCategorize";
	}
	@RequestMapping(value="/editcategorize", method=RequestMethod.POST)
	public String editCategorize(HttpServletRequest request,Model model){
		int categorizeId = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		Categorize categorize = this.categorizeService.getCategorizeById(categorizeId);
		categorize.setName(name);
		this.categorizeService.updateByPrimaryKeySelective(categorize);
		return "redirect:/categorize/listcategorize";
	}
	
	@RequestMapping(value="/addcategorize", method=RequestMethod.GET)
	public String addCategorizeForm(HttpServletRequest request,Model model){
		
		return "categorize/addCategorize";
	}
	
	@RequestMapping(value="/addcategorize", method=RequestMethod.POST)
	public String addCategorize(HttpServletRequest request,Model model){
		String name = new String(request.getParameter("name"));
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String add_date = new String(dateFormat.format(now));
		String modify_date = new String(add_date);
		
		int provider = 21;
		
		Categorize categorize = new Categorize();
		
		categorize.setName(name);
		categorize.setAddDate(add_date);
		categorize.setModifyDate(modify_date);
		categorize.setProvider(provider);
		
		this.categorizeService.insert(categorize);
		
		return "redirect:/categorize/listcategorize";
	}
	
	@RequestMapping("/listcategorize")
	public String listCategorize(HttpServletRequest request, Model model){
		List<Categorize> cates; 
		cates = this.categorizeService.getAllCategorize();
		model.addAttribute("list",cates);
		return "categorize/listCategorize";
	}
}
